
package org.argeproje.resim.proc;

import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import org.argeproje.resim.proc.data.ConverterUT;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ExtremaDA;
import org.argeproje.resim.proc.data.ImageDA;
import org.argeproje.resim.proc.math.ExtremaPR;



public class ThresholdPR extends IntensityTransformPR {
	protected double _threshold = 128.0;
	protected double _lowValue = 0.0;
	protected double _highValue = 255.0;
    public void setParameters(double threshold, double lowValue, double highValue){
    	setParamThreshold(threshold);
    	setParamLowValue(lowValue);
    	setParamHighValue(highValue);
    }
    public void setParamThreshold(double threshold){
    	_threshold = threshold;
    }
    public double getParamThreshold(){
    	return _threshold;
    }
    public void setParamLowValue(double lowValue){
    	_lowValue = lowValue;
    }
    public double getParamLowValue(){
    	return _lowValue;
    }
    public void setParamHighValue(double highValue){
    	_highValue = highValue;
    }
    public double getParamHighValue(){
    	return _highValue;
    }
	public Data process() {
		ExtremaPR extPR = new ExtremaPR();
		ExtremaDA extremaDA = (ExtremaDA)extPR.process(getInput());
		ParameterBlock pb = new ParameterBlock();
		int nBand = getInputAsImageDA().getNBands();
		double[] highArray = ConverterUT.get1DDoubleArray(getParamThreshold(), nBand);
		double[] destinationValue = ConverterUT.get1DDoubleArray(getParamLowValue(), nBand);
		pb.addSource(getInputAsPlanarImage());
		pb.add(extremaDA.getMinimas());
		pb.add(highArray);
		pb.add(destinationValue);
		PlanarImage outImg = JAI.create("threshold", pb);
		pb = new ParameterBlock();
		pb.addSource(outImg);
		pb.add(highArray);
		pb.add(extremaDA.getMaximas());
		destinationValue = ConverterUT.get1DDoubleArray(getParamHighValue(), nBand);
		pb.add(destinationValue);
		PlanarImage im = JAI.create("threshold", pb);
		ImageDA o = new ImageDA(im);
		setOutput(o);
		return getOutput();
	}

}
